﻿using WebSvrUpdater.Framework;
using NLog;
using System;
using System.ServiceProcess;
using System.Threading;

namespace WebSvrUpdater
{
    
    /// <summary>
    /// The actual implementation of the windows service goes here...
    /// </summary>
    [WindowsService("WebSvrUpdater",
        DisplayName = "满天星Web更新服务",
        Description = "用于满天星Web程序更新服务服务",
        EventLogSource = "WebSvrUpdater",
        StartMode = ServiceStartMode.Automatic)]
    public class WebSvrUpdaterService : IWindowsService
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        private Thread _workThread;

        public WebSvrUpdaterService()
        {
            
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {

        }

        /// <summary>
        /// This method is called when the service gets a request to start.
        /// </summary>
        /// <param name="args">Any command line arguments</param>
        public void OnStart(string[] args)
        {
            // Otherwise run as a windows service
            try
            {
                logger.Info("服务启动");
                
                //registry.Schedule<Job>().ToRunNow().AndEvery(30).Minutes();
                //JobManager.Initialize(registry);
            }
            catch (Exception e)
            {
                logger.Error(e, "OnStart异常");
            }
            
                    

        }

        /// <summary>
        /// This method is called when the service gets a request to stop.
        /// </summary>
        public void OnStop()
        {
            logger.Info("服务停止");
            //JobManager.Stop();
        }

        /// <summary>
        /// This method is called when a service gets a request to pause,
        /// but not stop completely.
        /// </summary>
        public void OnPause()
        {
            logger.Info("服务暂停");
            //JobManager.Stop();
        }

        /// <summary>
        /// This method is called when a service gets a request to resume 
        /// after a pause is issued.
        /// </summary>
        public void OnContinue()
        {
            logger.Info("服务恢复");
            //JobManager.Start();
        }

        /// <summary>
        /// This method is called when the machine the service is running on
        /// is being shutdown.
        /// </summary>
        public void OnShutdown()
        {
            logger.Info("服务已关闭");
            //JobManager.Stop();
        }

        /// <summary>
        /// This method is called when a custom command is issued to the service.
        /// </summary>
        /// <param name="command">The command identifier to execute.</param >
        public void OnCustomCommand(int command)
        {
        }
    }
}
